package com.jomoim.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * BaseAction - 모든 Action Class들은 BaseAction Class를 상속받아서 구현한다
 * @author JosephChoi
 *
 */
public class BaseAction extends ActionSupport implements SessionAware, ParameterAware {

	protected Map<String, String> parameters;
	
	protected Map resultMap;
	
	protected Map session; 
	
	protected InputStream inputStream; 
	
	protected Integer my_seq;
	
	public static final String ERROR404 = "error404";
	public static final String ERROR500 = "error500";
	
	public BaseAction() {
		resultMap = new HashMap();
	}
	
	@Override
	public void setParameters(Map parameters) {
		this.parameters = extractParameterMap(parameters);
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public InputStream getInputStream() {
		return inputStream;
	}
	
	public void setInputStream(String inputString) {
		try {
			this.inputStream = new ByteArrayInputStream(inputString.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	/**
	 * setInputStream(Map을 받아서 JSON형식으로 변환 후 inputStream으로 넘김)
	 * @param inputMap(String 값으로 되어 있어야 함)
	 */
	public void setInputStream(Map<String, String> inputMap){
//		Map defaultMap = new HashMap();
//		defaultMap.put("status_code", "200");
//		defaultMap.put("status_txt", "ok");
//		
//		JSONObject resultJSON = JSONObject.fromObject(defaultMap);
//		JSONObject subJSON = new JSONObject();
//		if(inputMap.size()>0){
//			Set keys = inputMap.keySet();
//			Iterator it = keys.iterator();
//			
//			while(it.hasNext()){
//				String key = (String)it.next();
//				String value = inputMap.get(key);
//				subJSON.element(key, value);
//			}
//		}
//		resultJSON.element("data", subJSON);
//		setInputStream(resultJSON.toString());
		
		JSONObject resultJSON = new JSONObject();
		resultJSON.element("status_code", "200");
		resultJSON.element("status_txt", "ok");
		
		JSONObject subJSON = JSONObject.fromObject(inputMap);
		resultJSON.element("data", subJSON);
		setInputStream(resultJSON.toString());
	}
	
	/**
	 * setInputStream(Object(Model)을 받아서 JSON형식으로 변환 후 inputStream으로 넘김)
	 * @param inputMap(String 값으로 되어 있어야 함)
	 */
	public void setInputStream(Object model){
		JSONObject resultJSON = new JSONObject();
		resultJSON.element("status_code", "200");
		resultJSON.element("status_txt", "ok");
		
		JSONObject subJSON = JSONObject.fromObject(model);
		resultJSON.element("data", subJSON);
		setInputStream(resultJSON.toString());
	}
	
	public Map getParameters() {
		return parameters;
	}
	
	/**
	 * 배열 인자가 하나인 것들은 스트링으로 바꿔준다
	 * @param parameterMap
	 * @return
	 */
	public Map<String, String> extractParameterMap(Map parameterMap){
		parameters = new HashMap<String, String>();
		if(parameterMap.size()>0){
			Set keys = parameterMap.keySet();
			
			Iterator it = keys.iterator();
			
			while(it.hasNext()){
				String key = (String)it.next();
				String[] values = (String[])parameterMap.get(key);
				parameters.put(key, values[0]);
			}
		}
		return parameters;
	}
	
	/**
	 * 배열 인자가 하나인 것들은 스트링으로 바꿔준다
	 * @param parameterMap
	 * @return
	
	public Map<String, String> extractParameterMap(Map parameterMap){
		if(parameterMap.size()>0){
			Set keys = parameterMap.keySet();
			
			Iterator it = keys.iterator();
			
			while(it.hasNext()){
				String key = (String)it.next();
				String[] values = (String[])parameterMap.get(key);
				if(values.length <= 1){
					parameterMap.put(key, values[0]);
				}
			}
		}
		return parameterMap;
	}
	*/
	
	public Integer getMy_seq() {
		return my_seq;
	}
	
	public Map getResultMap() {
		return resultMap;
	}
	
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
}
